PrepareLevelSelect:	
	bsr	FadeOut

	;; clear plane A VRAM C000
	move.l  #$40000003,d7
	bsr     ClearPlane64By28In64By32
	;; clear plane B VRAM E000
	move.l  #$60000003,d7
	bsr     ClearPlane64By28In64By32

	;; copy select map with border
	lea     StartLevelSelectBanner(pc),a5
	clr.w	d4
        moveq   #(StartRowLevelSelectBanner-StartLevelSelectBanner-1),d5
        moveq   #(((EndLevelSelectBanner-StartLevelSelectBanner)/(StartRowLevelSelectBanner-StartLevelSelectBanner))-1),d6
	;; VRAM C084 (plane A)
	move.l	#$40840003,d7
	bsr     CopyMapAAndMakeBorderB

	;; copy level names map
	lea     StartLevelSelectNames(pc),a5
	clr.w   d4
        moveq   #(StartRowLevelSelectNames-StartLevelSelectNames-1),d5
        moveq   #(((EndLevelSelectNames-StartLevelSelectNames)/(StartRowLevelSelectNames-StartLevelSelectNames))-1),d6
	;; VRAM C218
	move.l	#$42180003,d7
	;; number of levels - 1
	bsr     CopyMapWithSpaces
	
	;; make border for first entry in level names
	clr.w	d4
        moveq   #(StartRowLevelSelectNames-StartLevelSelectNames-1),d5
        moveq   #0,d6
	;; VRAM E196
	move.l	#$61960003,d7
	bsr	MakeBorder

	;; initialize sprites
        lea      StartSpriteLevelSelectInitial(pc),a5
	bsr	 SpriteLoad

	;; disable vint animate
	clr.l	VintAnimate

	bsr	FadeIn
	
	;; d0 has level index
	moveq	#0,d0
	;; d1 has old level index
	moveq	#0,d1
	
LevelSelectLoop:	
        bsr     WaitForVint
 	move.b	Port1Press,d2
 	btst	#TwoPlayerFlag,Flags
 	beq.s	LevelSelectSkipTwoPlayerPort
 	or.b	Port2Press,d2
LevelSelectSkipTwoPlayerPort:	
	;; d2 now has SACBRLDU (pressed)
	btst    #bitStart,d2
 	bne.s	LevelSelectJump
	tst.l	d0
	beq.s	LevelSelectCannotMoveUp
 	btst    #bitUp,d2
 	bne.s	LevelSelectMoveUp
LevelSelectCannotMoveUp:	
	cmpi.b	#(((EndLevelSelectJumpTable-StartLevelSelectJumpTable)/(StartRowLevelSelectJumpTable-StartLevelSelectJumpTable))-1),d0
	bge.s	LevelSelectCannotMoveDown
 	btst    #bitDown,d2
 	bne.s	LevelSelectMoveDown
LevelSelectCannotMoveDown:	
	bra.s	LevelSelectLoop
	
LevelSelectMoveUp:
	subq.l	#1,d0
	bra.s	LevelSelectMoveCommon
LevelSelectMoveDown:
	addq.l	#1,d0
LevelSelectMoveCommon:
	;; d0 has new level index
	;; d1 has old level index
	
	;; clear border on old level index
	clr.w	d4
	;; VRAM E196+0100*d1
	move.l	#($61960003),d7
	move.l	d1,d6
	move.b  #24,d5
	lsl.l	d5,d6
	add.l	d6,d7
 	;; subtract an extra 3 for the added whitespace
        moveq   #(StartRowLevelSelectNames-StartLevelSelectNames+1),d5
        moveq   #2,d6
 	bsr     ClearMap

	;; make border on new level index
	clr.w	d4
	;; VRAM E196+0100*d0
	move.l	#($61960003),d7
	move.l	d0,d6
	move.b  #24,d5
	lsl.l	d5,d6
	add.l	d6,d7
 	;; subtract an extra 3 for the added whitespace
        moveq   #(StartRowLevelSelectNames-StartLevelSelectNames-1),d5
        moveq   #0,d6
 	bsr     MakeBorder

	;; save new index to old index
	move.l	d0,d1
	
	bra.s   LevelSelectLoop

	;; d0.w has index of level to jump to
LevelSelectJump:
	;; shift 1 for word offsets, shift 2 for long offsets
	;; currently assumes all levels follow this level select code
	;; and not arbitarily loaded as needed
	lsl.w	#1,d0
	move.w	StartLevelSelectJumpTable(pc,d0.w),d1
	jmp	StartLevelSelectJumpTable(pc,d1.w)
	
;;; data used for level select
	
	;; only early levels are part of the demo
StartLevelSelectJumpTable:
	dc.w	(PrepareLevel0-StartLevelSelectJumpTable)
StartRowLevelSelectJumpTable:
	dc.w	(PrepareLevel1-StartLevelSelectJumpTable)
	dc.w	(PrepareLevel2-StartLevelSelectJumpTable)
	dc.w	(PrepareLevel3-StartLevelSelectJumpTable)
EndLevelDemoJumpTable:	
	dc.w	(PrepareLevel4-StartLevelSelectJumpTable)
	dc.w	(PrepareLevel5-StartLevelSelectJumpTable)
	dc.w	(PrepareLevel6-StartLevelSelectJumpTable)
	dc.w	(PrepareLevel7-StartLevelSelectJumpTable)
	dc.w	(PrepareLevel8-StartLevelSelectJumpTable)
EndLevelSelectJumpTable:	

StartLevelSelectNames:
	dc.b "0: LETTER PICKUP"
StartRowLevelSelectNames:
	dc.b "1: TRUCK SURFING"
	dc.b "2: LETTER CATCH "
	dc.b "3: TRUCK DRIVING"
	dc.b "4: LETTER GATHER"
	dc.b "5: BONUS LEVEL 0"
	dc.b "6: BONUS LEVEL 1"
	dc.b "7: BONUS LEVEL 2"
	dc.b "8: BONUS LEVEL 3"
EndLevelSelectNames:

StartSpriteLevelSelectInitial:
RileySpriteLevelSelectInitial:
	dc.b $01,$2C,$06,$01,$48,$80,$01,$E0
EndSpriteLevelSelectInitial:	

StartLevelSelectBanner:
	dc.b "RILEY",$27,"S LETTER RECYCLE: LEVEL SELECT"
StartRowLevelSelectBanner:
EndLevelSelectBanner:
